package org.example.usercontroller;

import lombok.extern.slf4j.Slf4j;
import org.example.pojo.PageBean;
import org.example.pojo.Result;
import org.example.pojo.entity.Order;
import org.example.pojo.userdto.OrderCheckDTO;
import org.example.pojo.uservo.OrderCheckVO;
import org.example.userservice.ShoppingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/user/order")
public class OrderController {

    @Autowired
    private ShoppingService shoppingService;

    @GetMapping
    public Result list(@RequestParam(defaultValue = "1") int page,
                       @RequestParam(defaultValue = "10") int pageSize,
                       @RequestParam(value = "state", required = false) int state) {
        OrderCheckDTO orderCheckDTO = new OrderCheckDTO();
        orderCheckDTO.setState(state);
        orderCheckDTO.setPage(page);
        orderCheckDTO.setPageSize(pageSize);
        if(orderCheckDTO.getPage() == null) {
            orderCheckDTO.setPage(1);
        }
        if (orderCheckDTO.getPageSize() == null) {
            orderCheckDTO.setPageSize(10);
        }
        log.info("orderCheckDTO:{}", orderCheckDTO);
        PageBean pageBean = shoppingService.getOrder(orderCheckDTO);
        OrderCheckVO orderCheckVO = new OrderCheckVO();
        orderCheckVO.setPageBean(pageBean);
        return Result.success(orderCheckVO);
    }
    @PutMapping("/pay")
    public Result pay(Long id) {
        shoppingService.pay(id);
        return Result.success();
    }
}
